package com.chinapost.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.UUID;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.chinapost.dao.BaseDaoI;
import com.chinapost.po.Tresource;
import com.chinapost.service.ResourceServiceI;
import com.chinapost.vo.Resource;
import com.chinapost.vo.Tree;

@Service("resourceService")
public class ResourceServiceImpl implements ResourceServiceI {

	private BaseDaoI<Tresource> resourceDao;

	public BaseDaoI<Tresource> getResourceDao() {
		return resourceDao;
	}

	@Autowired
	public void setResourceDao(BaseDaoI<Tresource> resourceDao) {
		this.resourceDao = resourceDao;
	}

	@Override
	public void delete(Resource resource) {
		del(resource.getCid());

	}

	private void del(String cid) {
		Tresource t = resourceDao.get(Tresource.class, cid);
		if (t != null) {
			Set<Tresource> resources = t.getTresources();
			if (resources != null && !resources.isEmpty()) {
				for (Tresource tresource : resources) {
					del(tresource.getCid());
				}
			}
			resourceDao.delete(t);
		}
	}

	@Override
	public void add(Resource resource) {
		Tresource t = new Tresource();
		BeanUtils.copyProperties(resource, t);
		t.setCid(UUID.randomUUID().toString());
		if (resource.getPid() != null) {
			t.setTresource(resourceDao.get(Tresource.class, resource.getPid()));
		}
		resourceDao.save(t);
	}

	@Override
	public void edit(Resource resource) {
		Tresource t = resourceDao.get(Tresource.class, resource.getCid());
		BeanUtils.copyProperties(resource, t);
		if (resource.getPid() != null && !resource.getPid().equals(resource.getCid())) {
			Tresource pt = resourceDao.get(Tresource.class, resource.getPid());
			if (pt != null) {
				if (isDown(t, pt)) {// 要将当前节点修改到当前节点的子节点中
					Set<Tresource> tresources = t.getTresources();// 当前要修改的节点的所有下级节点
					if (tresources != null && tresources.size() > 0) {
						for (Tresource tresource : tresources) {
							if (tresource != null) {
								tresource.setTresource(null);
							}
						}
					}
				}
				t.setTresource(pt);
			}

		}
	}


	private boolean isDown(Tresource t, Tresource pt) {
		if (pt.getTresource() != null) {
			if (pt.getTresource().getCid().equals(t.getCid())) {
				return true;
			} else {
				return isDown(t, pt.getTresource());
			}
		}
		return false;
	}

	@Override
	public List<Resource> treegrid(Resource resource) {
		String hql = "from Tresource t order by t.cseq";
		List<Tresource> tds = resourceDao.find(hql);
		return changeModel(tds);
	}
	
	private List<Resource> changeModel(List<Tresource> tresources){
		List<Resource> list = new ArrayList<Resource>();
		if (tresources!=null && tresources.size()>0){
			for (Tresource t : tresources){
				Resource r = new Resource();
				BeanUtils.copyProperties(t, r);
				if (t.getTresource()!=null){
					r.setPid(t.getTresource().getCid());
					r.setPname(t.getTresource().getCname());
				}
				list.add(r);
			}
		}
		return list;
	}

	/*
	private List<Tresource> getTreeById(String resourceId,List<Tresource> tresources){
		Tresource t = resourceDao.get(Tresource.class, resourceId);
		tresources.add(t);
		List<Tresource> list = new ArrayList<Tresource>(t.getTresources());
		if (list!=null && list.size()>0){
			for (Tresource tresource : list){
				getTreeById(tresource.getCid(), tresources);
			}
		}
		return tresources;
	}
  */
	@Override
	public List<Tree> comboTree() {
		String hql = "from Tresource t order by t.cseq";
		List<Tresource> list = resourceDao.find(hql);
		List<Tree> trees = new ArrayList<Tree>();
		if (list!=null && !list.isEmpty()){
			for (Tresource t : list){
				Tree tree= new Tree();
				tree.setId(t.getCid());
				tree.setText(t.getCname());
				if (t.getTresource()!=null){
					tree.setPid(t.getTresource().getCid());
				}
				trees.add(tree);
			}
		}
		return trees;
	}
	

}
